package com.heimao.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.text.SimpleDateFormat;

public class JsonUtil {
    public static String objectAsJson(Object obj){
       return objectAsJson(obj,"YYYY-MM-DD HH:mm:ss");
    }

    public static String  objectAsJson(Object obj,String dateFormat){
        ObjectMapper mp=new ObjectMapper();
        mp.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
        SimpleDateFormat fm=new SimpleDateFormat(dateFormat);
        mp.setDateFormat(fm);
        try{
            return mp.writeValueAsString(obj);
        }catch (JsonProcessingException e){
                e.printStackTrace();
        }
       return null;
    }
}
